﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HistoryManager
{
    public class HistoryManager : IHistoryManager
    {
        private StreamWriter m_streamWriter = null;
        private static string fileName = string.Empty;
        private string m_containingFolder = string.Empty;

        public HistoryManager(string userName, string p_folderPath)
        {
            fileName = userName + "_" + DateTime.Now.ToLongDateString() + "_" + DateTime.Now.ToLongTimeString();
            fileName = fileName.Replace(" ", "_").Replace(":", "_") + ".txt";

            fileName = Path.Combine(p_folderPath, fileName);

            if (!Directory.Exists(p_folderPath))
            {
                Directory.CreateDirectory(p_folderPath);
            }

            using (m_streamWriter = new StreamWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write)))
            {
                m_streamWriter.WriteLine("**** Session Started ****");
            }

            m_containingFolder = Environment.CurrentDirectory;
        }

        private void Write(string p_message)
        {
            using (m_streamWriter = new StreamWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write)))
            {
                m_streamWriter.WriteLine(p_message);
            }
        }

        #region IHistoryManager Members

        public void SaveHistory(string message)
        {
            Write(message);
        }

        public void OpenHistoryDirectory()
        {
            System.Diagnostics.Process.Start(m_containingFolder);
        }

        #endregion

        #region IHistoryManager Members


        public void OpenHistroyFile()
        {
            try
            {
                File.Open(fileName, FileMode.Open);
            }
            catch
            {

            }
        }

        #endregion
    }

    public interface IHistoryManager
    {
        void SaveHistory(string message);

        void OpenHistoryDirectory();

        void OpenHistroyFile();
    }
}
